package com.zt.weibo.main;

import com.zt.weibo.R;
import com.zt.weibo.interfaces.IWeiboPopListener;
import com.zt.weibo.vo.EnumSendType;
import com.zt.weibo.vo.StatusInfo;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 长按微博弹出的功能菜单类
 * 
 * @author zhaotong
 */
public class WeiboMenuPopwin
{
	private WeiboSendPopwin mSendPopwin = null;
	private PopupWindow mPopWin = null;
	private View mContentView = null;
	private View mParentView = null;
	private Context mContext = null;
	private StatusInfo mStatusInfo = null;
	private TextView mTitle = null;
	private TextView mRepost = null;
	private TextView mRepostOri = null;
	private TextView mComment = null;
	private TextView mShowInfo = null;

	/**
	 * 微博条目的弹出菜单
	 * @param con Context
	 * @param parentView 父View
	 * @param listener 回调接口，设置给发布弹窗
	 */
	public WeiboMenuPopwin(Context con, View parentView, IWeiboPopListener listener)
	{
		mContext = con;
		mParentView = parentView;
		mContentView = LayoutInflater.from(mContext).inflate(R.layout.weibo_press_menu, null);
		mPopWin = new PopupWindow(mContentView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mPopWin.setFocusable(true);
		mPopWin.setOutsideTouchable(false);
		mPopWin.setAnimationStyle(R.style.popwin_animation);
		mTitle = (TextView) mContentView.findViewById(R.id.weibo_menu_title);
		mRepost = (TextView) mContentView.findViewById(R.id.weibo_menu_repost);
		mRepostOri = (TextView) mContentView.findViewById(R.id.weibo_menu_repost_ori);
		mComment = (TextView) mContentView.findViewById(R.id.weibo_menu_comment);
		mShowInfo = (TextView) mContentView.findViewById(R.id.weibo_menu_userinfo);
		setListener();
		mSendPopwin = new WeiboSendPopwin(mContext, mParentView, listener);
	}

	private void setListener()
	{
		TouchListener touchListener = new TouchListener();
		ClickListener clickListener = new ClickListener();
		KeyListener keyListener = new KeyListener();
		mTitle.setOnKeyListener(keyListener);
		mRepost.setOnClickListener(clickListener);
		mRepostOri.setOnClickListener(clickListener);
		mComment.setOnClickListener(clickListener);
		mShowInfo.setOnClickListener(clickListener);

		mRepost.setOnTouchListener(touchListener);
		mRepostOri.setOnTouchListener(touchListener);
		mComment.setOnTouchListener(touchListener);
		mShowInfo.setOnTouchListener(touchListener);

	}

	/**
	 * 显示菜单，顺便传入微博数据类，微博数据类中已经包含了相应的作者数据类。
	 */
	public void show(StatusInfo statusInfo)
	{
		mTitle.requestFocus();
		mStatusInfo = statusInfo;
		try
		{
			mPopWin.showAtLocation(mParentView, Gravity.CENTER, 0, 0);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 消隐菜单
	 */
	public void dismiss()
	{
		try
		{
			if (null != mPopWin && mPopWin.isShowing())
			{
				mPopWin.dismiss();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private class ClickListener implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			switch (v.getId())
			{
			case R.id.weibo_menu_repost:
				mSendPopwin.setSendType(EnumSendType.E_SEND_REPOST, mStatusInfo);
				mSendPopwin.show();
				dismiss();
				break;
			case R.id.weibo_menu_repost_ori:
				mSendPopwin.setSendType(EnumSendType.E_SEND_REPOST_ORI, mStatusInfo);
				mSendPopwin.show();
				dismiss();
				break;
			case R.id.weibo_menu_comment:
				mSendPopwin.setSendType(EnumSendType.E_SEND_COMMENT, mStatusInfo);
				mSendPopwin.show();
				dismiss();
				break;
			case R.id.weibo_menu_userinfo:
				Toast.makeText(mContext, mContext.getString(R.string.toast_not_finish), 1000).show();
				dismiss();
				break;
			}
		}
	}

	private class TouchListener implements OnTouchListener
	{
		@Override
		public boolean onTouch(View v, MotionEvent event)
		{
			v.requestFocus();
			return false;
		}
	}

	private class KeyListener implements OnKeyListener
	{
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event)
		{
			if (keyCode == KeyEvent.KEYCODE_BACK)
			{
				dismiss();
				return true;
			}
			return false;
		}
	}

}
